home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 10 / tee.zip / TEE.ASM next >
Assembly Source File  |  1985-04-07  |  3KB  |  139 lines

  1.     name    tee
  2.     page    55,132
  3.     title    'TEE - tee junction for DOS pipes'
  4. ;
  5. ; TEE --- A "tee" junction for DOS 2.x pipes
  6. ;         after the fashion of the Unix function TEE  
  7. ; By A. K. Head, 6 Duffryn Place, Melbourne, Australia 3142
  8. ;    Somewhat overhauled & with error handling added by Ray Duncan
  9. ;
  10. ; TEE reads from the standard input (redirectable) and
  11. ; outputs to both the standard output (redirectable)
  12. ; and a file, e.g.:   DIR | TEE C:\MYDIR\FILE.EXT | SORT
  13. ;
  14. ; The file can be CON, LPT1, etc.  If it is CON, then
  15. ; keyboard Control-S pauses the display as usual.
  16.  
  17. command    equ    80h        ; buffer for command tail
  18. buflen    equ    16384        ; buffer length, alter to taste
  19.  
  20. cr    equ    0dh        ; ASCII carriage return
  21. lf    equ    0ah        ; ASCII line feed
  22. ff    equ    0ch        ; ASCII form feed
  23. eof    equ    01ah        ; End-of-file marker
  24. tab    equ    09h        ; ASCII tab code
  25. blank    equ    20h        ; ASCII blank
  26.  
  27.                 ; DOS 2.x pre-defined handles
  28. stdin    equ    0000        ; standard input file
  29. stdout    equ    0001        ; standard output file
  30. stderr    equ    0002        ; standard error file
  31. stdaux    equ    0003        ; standard auxilliary file
  32. stdprn    equ    0004        ; standard printer file
  33.  
  34.  
  35. cseg    segment para public 'CODE'
  36.  
  37.     assume    cs:cseg,ds:cseg
  38.  
  39.     org    100H        ; start .COM at 100H
  40.  
  41. tee    proc    far
  42.  
  43.     mov    cl,ds:command     ; get length of command tail.
  44.     xor    ch,ch 
  45.                 ; address of command string.
  46.     mov    si,offset command+1 
  47.     mov    di,offset command+1
  48.  
  49. tee1:                ; squeeze out leading spaces.
  50.     mov    al,byte ptr [si]
  51.     cmp    al,blank
  52.     jbe    tee2
  53.     mov    byte ptr [di],al
  54.     inc    di
  55.  
  56. tee2:    inc    si        ; look through command tail
  57.     loop    tee1        ; until it's exhausted.
  58.     mov    byte ptr [di],0 ; make ASCIIZ string.
  59.     mov    ah,3ch        ; create output file.
  60.     mov    cx,0        ; attribute=0.
  61.     mov    dx,offset command+1
  62.     int    21h
  63.     jc    tee6        ; can't create file
  64.     mov    handle,ax    ; save token for file.
  65.  
  66. tee3:    mov    ah,3fh        ; read standard input.
  67.     mov    bx,stdin
  68.     mov    cx,buflen
  69.     lea    dx,buffer
  70.     int    21h
  71.     jc    tee4        ; jump, error.
  72.     mov    nchar,ax    ; save length of read.
  73.     cmp    ax,0        ; nothing read in?
  74.     je    tee4        ; end of file, exit.
  75.     mov    ah,40h        ; write to file...
  76.     mov    bx,handle
  77.     mov    cx,nchar
  78.     int    21h
  79.     jc    tee7        ; jump, write failed.
  80.     cmp    ax,nchar
  81.     jne    tee7        ; jump, write failed.
  82.     mov    ah,40h        ; now write to standard output...
  83.     mov    bx,stdout
  84.     int    21h
  85.     jc    tee7        ; jump, write failed.
  86.     cmp    ax,nchar
  87.     jne    tee7        ; jump, write failed.
  88.     jmp     tee3        ; read again until end of file.
  89.  
  90. tee4:    mov    ah,3eh        ; close output file.
  91.     mov    bx,handle
  92.     int    21h
  93.  
  94. tee5:    mov    ax,4c00h    ; exit with return code=0.
  95.     int    21h
  96.  
  97. tee6:    mov    dx,offset err1    ; print "Can't create file".
  98.     mov    cx,err1len
  99.          mov    ah,40h        ; display error message and exit.
  100.     mov    bx,stderr
  101.     int    21h
  102.     mov    ax,4c01h    ; exit with return code=1.
  103.     int    21h
  104.  
  105. tee7:    mov    dx,offset err2    ; print "Disk is full".
  106.     mov    cx,err2len
  107.          mov    ah,40h        ; display error message and exit.
  108.     mov    bx,stderr
  109.     int    21h
  110.     mov    ah,3eh        ; close output file.
  111.     mov    bx,handle
  112.     int    21h
  113.     mov    ax,4c02h    ; exit with return code=2.
  114.     int    21h
  115.  
  116. tee    endp
  117.  
  118.  
  119. nchar    dw     0        ; number of chars actually input.
  120. handle    dw    0        ; token for output file.
  121.  
  122. err1       db    cr,lf
  123.     db    'tee: Cannot create file' 
  124.     db    cr,lf
  125. err1len equ    (this byte)-(offset err1) 
  126.  
  127. err2     db    cr,lf
  128.     db    'tee: Disk is full.'
  129.     db    cr,lf
  130. err2len equ    (this byte)-(offset err2) 
  131.  
  132. buffer    equ    this byte    ; data is read here from
  133.                 ; standard input
  134.  
  135. cseg    ends
  136.  
  137.     end    tee
  138.